﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using DAO;
using DTO;

namespace BUS
{
    public class PhieuNo_BUS
    {
        private static PhieuNo_BUS m_instance = null;

        public static PhieuNo_BUS Instance
        {
            get
            {
                if (m_instance == null)
                    m_instance = new PhieuNo_BUS();
                return m_instance;
            }
        }

        /// <summary>
        /// Lấy mã phiếu nợ lớn nhất
        /// </summary>
        /// <returns></returns>
        public string LayMaPhieuNoMax()
        {
            DataTable dt = PhieuNo_DAO.Instance.LayMaPhieuNoMax();
            // Bi loi
            if (dt == null)
                return null;

            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
                return "n/a";

            DataRow row = list[0];
            string maphieu = (string)row["MaPhieuNo"];

            return maphieu;
        }

        /// <summary>
        /// Lấy phiếu nợ cuối cùng của 1 phiếu bán hàng
        /// </summary>
        /// <param name="mapb"></param>
        /// <returns></returns>
        public PhieuNo_DTO LayPhieuNoCuoi(string mapb)
        {
            DataTable dt = PhieuNo_DAO.Instance.LayPhieuNoCuoi(mapb);
            // Bi loi
            if (dt == null)
                return null;

            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
                return null;

            DataRow row = list[0];
            string maphieu = (string)row["MaPhieuNo"];
            DateTime ngNo = (DateTime)row["NgayNo"];
            DateTime ngTra = (DateTime)row["NgayTra"];
            decimal temp = (decimal)row["SoTienTraNo"];
            long soTienNo = (long)temp;
            temp = (decimal)row["SoTienTra"];
            long soTienTra = (long)temp;
            temp = (decimal)row["ConLai"];
            long soTienConLai = (long)temp;
            return new PhieuNo_DTO(maphieu, mapb, ngNo, ngTra, soTienNo, soTienTra, soTienConLai);
        }

        /// <summary>
        /// Thêm phiếu nợ mới
        /// </summary>
        /// <param name="phieu"></param>
        /// <returns></returns>
        public bool ThemPhieuNoMoi(PhieuNo_DTO phieu)
        {
            return PhieuNo_DAO.Instance.ThemPhieuNoMoi(phieu.MaPhieuBanHang, phieu.NgayNo.ToString(), phieu.SoTienNo, phieu.NgayTra.ToString(), phieu.SoTienTra, phieu.ConLai);
        }

        public List<PhieuNo_DTO> LayDanhSachPhieuNo(string mapbh)
        {
            List<PhieuNo_DTO> result = new List<PhieuNo_DTO>();

            DataTable dt = PhieuNo_DAO.Instance.LayToanBoPhieuNo(mapbh);
            if (dt == null)
                return null;

            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
                return result;
            for (int i = 0; i < list.Count; i++)
            {
                DataRow row = list[i];
                string mapn = (string)row["MaPhieuNo"];
                //string mapbh = (string)row["MaPhieuBanHang"];
                DateTime ngNo = (DateTime)row["NgayNo"];
                DateTime ngTra = (DateTime)row["NgayTra"];
                decimal temp = (decimal)row["SoTienTraNo"];
                long tienNo = (long)temp;
                temp = (decimal)row["SoTienTra"];
                long tienTra = (long)temp;
                temp = (decimal)row["ConLai"];
                long conLai = (long)temp;
                result.Add(new PhieuNo_DTO(mapn, mapbh, ngNo, ngTra, tienNo, tienTra, conLai));
            }
            return result;
        }
    }
}
